<template>
  <a-tabs v-model:active-key="cur" size="mini" @change="tabChange">
    <a-tab-pane key="1" title="按任务甲方汇总">
      <div class="tab-con publish-task">
        <div class="common-text mb5 clearfix top">
          <div class="fl">
            <span>数据统计时间：</span>
            <span>{{ $setTime(time) }}</span>
          </div>
          <div class="fr">
            <a-button class="gray-outline" @click="upload">导出</a-button>
          </div>
        </div>
        <div class="bottom">
          <a-table :data="punlishTaskDataList" class="table-two-line" :pagination="false" :summary="summary" :scroll="{ y: '100%' }">
            <template #columns>
              <a-table-column title="任务甲方" #cell="{ record, rowIndex }" dataIndex="partyaCompanyShortName">
                {{ record.consumerAccountUserName }}
              </a-table-column>
              <a-table-column title="待抢单任务" #cell="{ record }" dataIndex="waitGrapNum">
                {{ record.waitGrapNum }}
              </a-table-column>
              <a-table-column title="待接单任务" #cell="{ record }" dataIndex="waitAcceptNum">
                {{ record.waitAcceptNum }}
              </a-table-column>
              <a-table-column title="送样中任务" #cell="{ record }" dataIndex="sendingSampleNum">
                {{ record.sendingSampleNum }}
              </a-table-column>
              <a-table-column title="待执行任务" #cell="{ record }" dataIndex="waitScheduleNum">
                {{ record.waitScheduleNum }}
              </a-table-column>
              <a-table-column title="执行中任务" #cell="{ record }" dataIndex="executingNum">
                {{ record.executingNum }}
              </a-table-column>
              <a-table-column title="待审核任务" #cell="{ record }" dataIndex="waitCheckNum">
                {{ record.waitCheckNum }}
              </a-table-column>
              <a-table-column title="待验收任务" #cell="{ record }" dataIndex="waiteVerifyNum">
                {{ record.waiteVerifyNum }}
              </a-table-column>
              <a-table-column title="已完成任务" #cell="{ record }" dataIndex="finishedNum">
                {{ record.finishedNum }}
              </a-table-column>
            </template>
            <template #summary-cell="{ column, record, rowIndex }">
              <div style="font-size: 16px; font-weight: 700">{{ record[column.dataIndex] }}</div>
            </template>
          </a-table>
        </div>
      </div>
    </a-tab-pane>
    <a-tab-pane key="2" title="按执行人汇总">
      <user v-if="cur == '2'" @save="openDownLoadTitle = true" />
    </a-tab-pane>
    <a-tab-pane key="3" title="已完成任务明细">
      <all v-if="cur == '3'" />
    </a-tab-pane>
  </a-tabs>

  <a-modal v-model:visible="openDownLoadTitle" width="400px" :footer="false" title="提示" title-align="start" :mask-closable="false">
    <div style="text-align: center">
      <p class="text-c" style="font-size: 16px">您的导出任务已发起，稍后前往下载中心下载</p>
      <a-button style="margin-top: 20px" type="primary" status="success" @click="toDownLoad">前往下载中心</a-button>
    </div>
  </a-modal>
</template>

<script lang="ts" setup>
  import { defineComponent, ref, onMounted } from 'vue'
  import { findTaskPartyaStatvo, exportTaskPartyaStat } from '@/api/task'
  import { useRouter } from 'vue-router'
  import all from './components/all.vue'
  import user from './components/user.vue'
  const router = useRouter()
  let cur = ref('1')
  let time = ref('')
  let openDownLoadTitle = ref(false)
  let punlishTaskDataList: any = ref([])

  const getList = async () => {
    const res = await findTaskPartyaStatvo()
    if (res) {
      time.value = res?.data?.gmtLastUpdate || new Date()
      punlishTaskDataList.value = res?.data?.statList || []
    }
  }

  const summary = () => {
    let countData = {
      waitGrapNum: 0,
      waitAcceptNum: 0,
      sendingSampleNum: 0,
      waitScheduleNum: 0,
      executingNum: 0,
      waitCheckNum: 0,
      waiteVerifyNum: 0,
      finishedNum: 0,
    }
    punlishTaskDataList.value.forEach((record: any) => {
      countData.waitGrapNum += record.waitGrapNum
      countData.waitAcceptNum += record.waitAcceptNum
      countData.sendingSampleNum += record.sendingSampleNum
      countData.waitScheduleNum += record.waitScheduleNum
      countData.executingNum += record.executingNum
      countData.waitCheckNum += record.waitCheckNum
      countData.waiteVerifyNum += record.waiteVerifyNum
      countData.finishedNum += record.finishedNum
    })

    return [
      {
        partyaCompanyShortName: '合计',
        ...countData,
      },
    ]
  }

  // 前往下载中心；
  function toDownLoad() {
    openDownLoadTitle.value = false
    router.push('/system/download/download')
  }

  const upload = async () => {
    const res = await exportTaskPartyaStat()
    if (res) {
      openDownLoadTitle.value = true
    }
  }

  onMounted(() => {
    getList()
  })
</script>

<style lang="less" scoped>
  :deep(.arco-tabs-nav-tab) {
    background-color: white;
  }

  .common-text {
    font-size: 12px;
    color: #a39999;
  }

  .tab-con {
    height: calc(100vh - @menuAndTabHeight - 5px);
    background: white;
    margin-top: 5px;
    padding: 10px 20px;
    box-sizing: border-box;
  }

  .publish-task {
    display: flex;
    flex-direction: column;

    .bottom {
      flex: 1;
      flex-direction: column;
      overflow-y: hidden;
    }
  }
</style>
